{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7\n"
     ]
    }
   ],
   "source": [
    "#编辑距离是用来比较两个字符串之间相似度的度量方法，表示的是两个字符串间相互转换所需的最少步骤。\n",
    "import numpy as np\n",
    "\n",
    "def edit_distance(str1, str2):\n",
    "    dp = np.zeros((len(str1)+1, len(str2)+1))\n",
    "    for i in range(len(str1) + 1):\n",
    "        dp[i][0] = i\n",
    "    for j in range(len(str2) + 1):\n",
    "        dp[0][j] = j\n",
    "\n",
    "    for i in range(1, len(str1) + 1):\n",
    "        for j in range(1, len(str2) + 1):\n",
    "            temp = 0 if str1[i - 1] == str2[j - 1] else 1\n",
    "            dp[i][j] = min(dp[i - 1][j - 1] + temp, min(dp[i - 1][j] + 1, dp[i][j - 1] + 1))\n",
    "    return dp[len(str1)][len(str2)]\n",
    "\n",
    "def simility(str1, str2):\n",
    "    # 假设句子1包含句子2 相似度为1\n",
    "    if len(str1)>len(str2):\n",
    "        if str2 in str1:\n",
    "            return 1.0\n",
    "    else:\n",
    "        if str1 in str2:\n",
    "            return 1.0\n",
    "    res = edit_distance(str1, str2)\n",
    "    maxLen = max(len(str1),len(str2))\n",
    "    return 1-res*1.0/maxLen\n",
    "\n",
    "res = simility('中国男子篮球职业联赛','中男篮职业联赛')\n",
    "print(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import distance\n",
    "\n",
    "def edit_distance(s1, s2):\n",
    "    return distance.levenshtein(s1, s2)\n",
    "\n",
    "s1 = 'xiaoming'\n",
    "s2 = 'xiamin'\n",
    "print(str(edit_distance(s1, s2)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
